package vsvm.gui;

import java.awt.Dimension;

import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import vsvm.gui.controls.TaskBarButton;

public class VSVMInternalFrame extends JInternalFrame{
	private static final long serialVersionUID = 100L;
	
	public static final int STATE_MINIMIZED =  0;
	public static final int STATE_RESTORED  =  1;
	public static final int STATE_UNKNOWN   = -1;
	
	private TaskBarButton button = null;
	private int state = STATE_UNKNOWN;
		
	public VSVMInternalFrame(String title) {
		super(title, true, true, true, true);
		
		this.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		this.setSize(new Dimension(400, 300));
		
		this.addInternalFrameListener(new InternalFrameAdapter(){
			public void internalFrameIconified(InternalFrameEvent e){
				
				if (button != null) {
					button.setSelected(false);
					VSVMInternalFrame.this.setState(VSVMInternalFrame.STATE_MINIMIZED);
					VSVMInternalFrame.this.setVisible(false);					
				}
			}
			
			public void internalFrameOpened(InternalFrameEvent e) {
				VSVMInternalFrame.this.setState(VSVMInternalFrame.STATE_RESTORED);				
				Environment.getEnvironment().getTaskBar().addWindow(VSVMInternalFrame.this);
				Environment.getEnvironment().getMainFrame().updateWindowMenu();
			}
			
			public void internalFrameDeactivated(InternalFrameEvent e) {
				if (button != null) 
					button.setSelected(false);				    
			}
			
			public void internalFrameActivated(InternalFrameEvent e) {
				if (button != null && !button.isSelected()) 
					button.setSelected(true);
			}	
			
			public void internalFrameClosed(InternalFrameEvent e) {
				Environment.getEnvironment().getTaskBar().removeWindow(VSVMInternalFrame.this);
				Environment.getEnvironment().getMainFrame().updateWindowMenu();
			}
			
			public void internalFrameDeiconified(InternalFrameEvent e){
				
				VSVMInternalFrame.this.setState(VSVMInternalFrame.STATE_RESTORED);
				VSVMInternalFrame.this.moveToFront();
				
			}
			
		});		
	}
	
	public TaskBarButton getTaskBarButton() {
		return button;
	}
	
	public void setTaskBarButton(TaskBarButton button) {
		this.button = button;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return state;
	}
	
	
}
